# Contributor: Jakub Jirutka <jakub@jirutka.cz>
# Maintainer: Jakub Jirutka <jakub@jirutka.cz>
pkgname=lua-copas
_rockname=copas
pkgver=4.7.1
_pkgver=${pkgver//./_}
pkgrel=0
pkgdesc="Coroutine Oriented Portable Asynchronous Services for Lua"
url="https://lunarmodules.github.io/copas/"
arch="noarch"
license="MIT"
depends="
	lua-binaryheap
	lua-coxpcall
	lua-socket
	lua-timerwheel
	"
checkdepends="lua-sec"
source="$pkgname-$pkgver.tar.gz::https://github.com/keplerproject/$_rockname/archive/v$_pkgver.tar.gz
	ipv4-only.patch
	"
builddir="$srcdir/$_rockname-$_pkgver"

case "$CARCH" in
	ppc64le|riscv64|loongarch64) _luajit="";;
	*) checkdepends="$checkdepends luajit"; _luajit="jit";;
esac

_luaversions="5.1 5.2 5.3 5.4"
for _v in $_luaversions; do
	checkdepends="$checkdepends lua$_v"
	# for backward compatibility (Alpine <3.18)
	provides="$provides lua$_v-${pkgname#lua-}=$pkgver-r$pkgrel"
done

prepare() {
	default_prepare

	# FIXME: Theses tests fail, most likely due to lua-socket >=3.0, see
	#  https://github.com/lunarmodules/copas/issues/159.
	#  I don't know if it affects only tests or copas is broken now. :(
	sed -i \
		-e '/tests\/httpredirect.lua/d' \
		-e '/tests\/largetransfer.lua/d' \
		-e '/tests\/starve.lua/d' \
		-e '/tests\/tls-sni.lua/d' \
		Makefile
}

check() {
	local lver; for lver in $_luaversions $_luajit; do
		msg "Testing on lua$lver"
		make test LUA=lua$lver
	done
}

package() {
	local rockdir="$pkgdir/usr/lib/luarocks/rocks-common/$_rockname/$pkgver-1"

	cd "$builddir"
	make LUA_DIR="$pkgdir/usr/share/lua/common" install

	mkdir -p "$rockdir"
	echo 'rock_manifest = {}' > "$rockdir"/rock_manifest
}

sha512sums="
906e7eeabdd343fd0c4ab03cc13b868981cdc3294e733fa1d7641c2a1b4de031fec8dda2c8e47a2bbfdd490f2864d352fc6b4c34274a74f3926a6fcb3fd96132  lua-copas-4.7.1.tar.gz
20acb34bc3cd0e974d2b7c9e54ade4e29cfa3e5dd7d52351bb0e06760301dc354c964c450080d61df2ebb3577e9407caea24582fbd9d2576485ae4aad7bafcf7  ipv4-only.patch
"
